/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.demo.soap.server.service.impl;

import it.txt.access.capability.demo.soap.server.service.CapabilityServiceException;
import it.txt.access.capability.commons.utils.ClientServerUtils;
import it.txt.access.capability.demo.schema.CapabilityRequestType;
import it.txt.access.capability.demo.schema.CapabilityResponseType;
import it.txt.access.capability.demo.soap.server.model.ServerResponseInfoModel;
import it.txt.access.capability.demo.soap.server.model.ServiceRequestInfoModel;
import it.txt.access.capability.demo.soap.server.service.CapabilityService;
import it.txt.access.capability.demo.schema.factory.CapabilityDemoSchemaFactoryException;
import it.txt.access.capability.demo.schema.factory.CapabilityDemoSchemaFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.dom.DOMSource;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;

/**
 *
 * @author seccia
 */
@Service
public class CapabilityServiceImpl implements CapabilityService{

    private static final Logger LOGGER = Logger.getLogger(CapabilityServiceImpl.class.getName());

    private static final CapabilityServiceHelper HELPER = new CapabilityServiceHelper();

    @Override
    public CapabilityResponseType capabilityAccessRequest(CapabilityRequestType crt) throws CapabilityServiceException {

        //the response message sent to the client
        String responseMessage = "";
        //init a new server response info model
        ServerResponseInfoModel responseInfoModel = new ServerResponseInfoModel();        
        try {
            //check the received client request.
            ServiceRequestInfoModel requestInfoModel = HELPER.checkRequestCapability(crt);
            //Strore the request ID into the response info model.
            responseInfoModel.setRequestID(requestInfoModel.getRequestID());
            //Store the vslid request status into the response info model.
            responseMessage += ClientServerUtils.RESPONSE_CAPABILITY_VALID;
            try {
                //Now try to perform the requested operation
                HELPER.performRequestOperation(requestInfoModel);
                //The operation was performed. Append to the status response
                //the operation performed message.
                responseMessage += " " + ClientServerUtils.RESPONSE_REQUEST_OPERATION_PERFORMED;
            }
            catch (CapabilityServiceException ex) {
                //The request contains an invalid operation.
                LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
                //Store the error response status into the response info model.
                responseMessage += " " + ex.getMessage();
            }
        }
        catch (CapabilityServiceException ex) {
            //The request contains an invalid capability.
            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
            //Store invalid request ID into the response info model.
            responseInfoModel.setRequestID(ClientServerUtils.INVALID_REQUEST_ID);
            //Store the error response status into the response info model.
            responseMessage = ClientServerUtils.RESPONSE_CAPABILITY_INVALID;
            responseMessage += " " + ex.getMessage();
        }
        //Store the final request status into the response info model.
        responseInfoModel.setResponseStatus(responseMessage);
        try {
            //Recover the response
            Element signedResponse = HELPER.createSignedResponse(responseInfoModel);
            return CapabilityDemoSchemaFactory.getCapabilityResponseFromSource(
                    new DOMSource(signedResponse));
        }
        catch (CapabilityDemoSchemaFactoryException ex) {
            throw new CapabilityServiceException(ex.getMessage(), ex);
        }
    }

    @Override
    public Element capabilityAccessRequest(Element request) throws CapabilityServiceException {
        try {
            //Recover the Capability as an Object
            CapabilityRequestType capabilityRequest =
                    CapabilityDemoSchemaFactory.getCapabilityRequestFromSource(
                    new DOMSource(request));
            //Recover the Capability response as an Object
            CapabilityResponseType capabilityResponse =
                    capabilityAccessRequest(capabilityRequest);
            //Return the Capability response as an Element
            return CapabilityDemoSchemaFactory.getCapabilityResponseToDocument(
                    capabilityResponse).getDocumentElement();
        }
        catch (CapabilityDemoSchemaFactoryException ex) {
            throw new CapabilityServiceException(ex.getMessage(), ex);
        }
    }
}
